package me.zhengjie.modules.device.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.device.domain.PeopleDevice;
import me.zhengjie.modules.device.service.PeopleDeviceService;
import me.zhengjie.modules.device.service.dto.PeopleDeviceDTO;
import me.zhengjie.modules.device.service.query.PeopleDeviceQueryService;
import me.zhengjie.utils.SecurityContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;

/**
* @author 张鹏
* @date 2019-05-07
*/
@RestController
@RequestMapping("api")
public class PeopleDeviceController {

    @Autowired
    private PeopleDeviceService peopleDeviceService;

    @Autowired
    private PeopleDeviceQueryService peopleDeviceQueryService;

    private static final String ENTITY_NAME = "peopleDevice";

    @Log("查询PeopleDevice")
    @GetMapping(value = "/peopleDevice")
    //@PreAuthorize("hasAnyRole('ADMIN')")
    public ResponseEntity getPeopleDevices(PeopleDeviceDTO resources, Pageable pageable){
        return new ResponseEntity(peopleDeviceQueryService.queryAll(resources,pageable),HttpStatus.OK);
    }

    @Log("新增PeopleDevice")
    @PostMapping(value = "/peopleDevice")
    //@PreAuthorize("hasAnyRole('ADMIN')")
    public ResponseEntity create(@Validated @RequestBody PeopleDevice resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        UserDetails userDetails = SecurityContextHolder.getUserDetails();
        resources.setCreator(userDetails.getUsername());
        resources.setUpdator(userDetails.getUsername());
        Date now = new Date();
        resources.setCreateTime(new Timestamp(now.getTime()));
        resources.setUpdateTime(new Timestamp(now.getTime()));
        return new ResponseEntity(peopleDeviceService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改PeopleDevice")
    @PutMapping(value = "/peopleDevice")
    //@PreAuthorize("hasAnyRole('ADMIN')")
    public ResponseEntity update(@Validated @RequestBody PeopleDevice resources){
        if (resources.getId() == null) {
            throw new BadRequestException(ENTITY_NAME +" ID Can not be empty");
        }
        UserDetails userDetails = SecurityContextHolder.getUserDetails();
        resources.setUpdator(userDetails.getUsername());
        Date now = new Date();
        resources.setUpdateTime(new Timestamp(now.getTime()));
        peopleDeviceService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除PeopleDevice")
    @DeleteMapping(value = "/peopleDevice/{id}")
    //@PreAuthorize("hasAnyRole('ADMIN')")
    public ResponseEntity delete(@PathVariable Long id){
        peopleDeviceService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}